import 'package:cbec_app/components/base/text_nav_item.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TextNav extends StatefulWidget {
  ///列表
  final List<String> list;

  ///文字导航栏
  const TextNav({
    super.key,
    required this.list,
  });

  @override
  State<StatefulWidget> createState() => _TextNavState();
}

class _TextNavState extends State<TextNav> {
  ///当前激活的索引
  var curIndex = 0;

  ///生成widget列表
  List<Widget> _createList() {
    List<Widget> arr = [];
    for (int i = 0; i < widget.list.length; i++) {
      var text = widget.list[i];
      arr.add(TextNavItem(
        text: text,
        index: i,
        curIndex: curIndex,
        onClick: (index) {
          logger.d('父组件 $index');
          setState(() {
            curIndex = index;
          });
        },
      ));
    }
    return arr;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(child: Center(
      child: Container(
          width: 359.w,
          height: 18.h,
          margin: EdgeInsets.only(top: 14.h,bottom: 10.h),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: _createList())),
    ))
      ],
    );
  }
}
